<template>
  <div>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="角色名称" v-model="roleNames.roleName" />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      <el-button @click="resetSearch">清空</el-button>
    </el-form>

    <div style="margin: 20px 0">
      <el-button type="primary" @click="addRole">添加</el-button>
      <el-button type="danger" @click="removeRoles" :disabled="selectedRoles.length === 0">批量删除</el-button>
    </div>

    <el-table border stripe style="width: 960px" :data="roles" @selection-change="handleSelectionChange">

      <el-table-column type="selection" width="55" />

      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>

      <el-table-column label="角色名称">
        <template v-slot="{ row }">
          <template v-if="row.edit">
            <el-input v-model="row.roleName" class="edit-input" size="small" />
            <el-button @click="cancelEdit(row)" class="cancel-btn" size="mini" type="warning" icon="el-icon-refresh">
              取消
            </el-button>
          </template>
          <span v-else>{{ row.roleName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="300" align="center">
        <template v-slot="{ row }">
          <el-button size="mini" @click="jump(row)" type="info" title="分配权限" icon="el-icon-info" />

          <el-button size="mini" @click="updateRole(row)" type="primary" title="确定" icon="el-icon-check" />
          <el-button size="mini" @click="row.edit = true" type="primary" title="修改角色" icon="el-icon-edit-outline" />
          <el-button size="mini" type="danger" title="删除角色" icon="el-icon-delete" @click="removeRole(row)" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
        :page-sizes="[5, 10, 15]" :page-size="5" layout="prev, pager, next, jumper, ->, sizes, total" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      page: 1,
      limit: 5,
      total: 0,
      roleNames: {
        roleName: '',
        searchRoleName: '',
      },
      selectedRoles: [],
      listLoading: false,
      roles: []
    }
  },
  methods: {
    // 获取页面数据
    async getRoles() {
      let page = this.page
      let limit = this.limit
      let searchRoleName = this.roleNames.searchRoleName
      try {
        let result = await this.$API.role.getPageList(page, limit, searchRoleName)
        console.log(result)
        this.roles = result.data.items.map(item => {
          item.edit = false
          item.originRoleName = item.roleName
          return item
        })
        this.total = result.data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.limit = val
      this.page = 1
      this.getRoles()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val
      this.getRoles()
    },

    // 查询
    search() {
      this.roleNames.searchRoleName = this.roleNames.roleName
      this.getRoles()
      this.roleNames.roleName = ''
    },
    // 清空
    resetSearch() {
      this.roleNames.roleName = ''
      this.roleNames.searchRoleName = ''
      this.getRoles()
    },

    // 添加
    addRole() {
      this.$prompt('请输入新名称', '添加角色', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(async ({ value }) => {
        await this.$API.role.save({ roleName: value })
        this.$message({
          type: 'success',
          message: "添加角色成功"
        });
        this.getRoles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消添加'
        });
      });
    },

    // 当表格复选框选项发生变化的时候触发
    handleSelectionChange(selection) {
      this.selectedRoles = selection
    },
    // 批量删除
    async removeRoles() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const ids = this.selectedRoles.map(role => role.id)
        await this.$API.role.removeRoles(ids)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getRoles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    // 单个删除
    async removeRole(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$API.role.removeById(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getRoles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    // 取消
    cancelEdit(role) {
      // console.log(role)
      role.roleName = role.originRoleName
      role.edit = false
      this.$message({
        type: 'success',
        message: '取消角色修改'
      });
    },

    // 确认
    async updateRole(role) {
      try {
        await this.$API.role.updateById(role)
        this.$message({
          type: 'success',
          message: '更新角色成功'
        });
        this.getRoles()
      } catch (error) {
        console.log(error)
      }
    },

    // 跳转
    jump(row) {
      console.log(row)
      this.$router.push(`/acl/role/auth?id=${row.id}&roleName=${row.roleName}`)
    }


  },
  mounted() {
    this.getRoles()
  }
}
</script>
<style></style>
